package 数据结构.树.二分搜索树;

import java.util.*;

/**
 * @author Veng Su 1344114844@qq.com
 * @date 2018/7/21 16:16
 */
public class BST<E extends Comparable<E>> {
    private class Node {
        E e;
        Node left, right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }
    }

    Node root;
    int size;

    public BST() {
        root = null;
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(E e) {
        root = add(root, e);
        size++;
    }

    private Node add(Node node, E e) {
        if (node == null)
            return new Node(e);
        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        }
        return node;

    }

    public boolean contains(E e) {
        return contains(root, e);
    }

    private boolean contains(Node node, E e) {
        if (node == null) {
            return false;
        }
        if (e.compareTo(node.e) == 0) {
            return true;
        } else if (e.compareTo(node.e) < 0) {
            return contains(node.left, e);
        } else {
            return contains(node.right, e);
        }
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        preOrder(root);
    }

    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    /**
     * 非递归前序遍历
     */
    public void preOrderNR() {
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node cur = stack.pop();
            System.out.println(cur.e);
            if (cur.right != null)
                stack.push(cur.right);
            if (cur.left != null)
                stack.push(cur.left);
        }
    }

    /**
     * 中序遍历
     */
    public void inOrder() {
        inOrder(root);
    }

    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    public void inOrderNR() {
        Stack<Node> stack = new Stack<>();
        Node cur = root;
        while (!stack.isEmpty() || cur != null) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();
                System.out.println(cur.e);
                cur = cur.right;
            }
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder() {
        postOrder(root);
    }

    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    public void postOrderNR() {
        List<E> resultList = new ArrayList<>();
        if (root == null)
            return;
        Map<Node, Integer> visitedMap = new HashMap<>();
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node tempNode = stack.peek(); //注意这里是peek而不是pop
            if (tempNode.left == null && tempNode.right == null) { //如果没有左右孩子则访问
                resultList.add(tempNode.e);
                visitedMap.put(tempNode, 1);
                stack.pop();
                continue;
            } else if (!((tempNode.left != null && visitedMap.get(tempNode.left) == null) || (tempNode.right != null && visitedMap.get(tempNode.right) == null))) {
                //如果节点的左右孩子均已被访问
                resultList.add(tempNode.e);
                stack.pop();
                visitedMap.put(tempNode, 1);
                continue;
            }
            if (tempNode.left != null) {
                while (tempNode.left != null && visitedMap.get(tempNode.left) == null) {//左孩子没有被访问
                    stack.push(tempNode.left);
                    tempNode = tempNode.left;
                }
            }
            if (tempNode.right != null) {
                if (visitedMap.get(tempNode.right) == null) {//右孩子没有被访问
                    stack.push(tempNode.right);
                }
            }
        }
        System.out.println(resultList);
    }

    /**
     * 层序遍历
     */
    public void levelOrder() {
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node cur = queue.remove();
            System.out.println(cur.e);

            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }
    }

    /**
     * 查找最小值
     *
     * @return 最小值
     */
    public E minium() {
        if (size == 0) {
            throw new IllegalArgumentException("二叉树为空");
        }
        return minium(root).e;
    }

    private Node minium(Node node) {
        if (node.left == null) {
            return node;
        } else {
            return minium(node.left);
        }
    }

    /**
     * 查找最大值
     *
     * @return 最大值
     */
    public E maxium() {
        if (size == 0) {
            throw new IllegalArgumentException("二叉树为空");
        }
        return maxium(root);
    }

    private E maxium(Node node) {
        if (node.right == null) {
            return node.e;
        } else return maxium(node.right);
    }

    /**
     * 删除最小值
     *
     * @return 最小值
     */
    public E removeMin() {
        E ret = minium();

        root = removeMin(root);
        return ret;
    }

    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    /**
     * 删除最大值
     *
     * @return 最大值
     */
    public E removeMax() {
        E ret = maxium();
        root = removeMax(root);
        return ret;

    }

    private Node removeMax(Node node) {
        if (node.right == null) {
            Node leftNode = node.left;
            node.left = null;
            size--;
            return leftNode;
        }

        node.right = removeMax(node.right);
        return node;
    }

    public void remove(E e) {
        root=remove(root,e);
    }

    private Node remove(Node node, E e) {
        if (node == null) {
            return node;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = remove(node.left, e);
            return node;
        } else if (e.compareTo(node.e) > 0) {
            node.right = remove(node.right, e);
            return node;
        } else {
            if (node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }
            if (node.right == null) {
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }
            Node sucessor = minium(node.right);
            sucessor.right = removeMin(node.right);
            sucessor.left = node.left;
            node.left = null;
            node.right = null;
            return sucessor;
        }


    }
}
